package vis.items.robot;

import java.util.Random;

import processing.core.PImage;
import vis.items.visualItem.AbstractSketch;

public class RobotSketch extends AbstractSketch {
	private static final long serialVersionUID = -8178889650670232322L;

	private static final int FRAME_RATE = 1;
	private static final int MOVE_STEP = 32;
	private Random rnd;
	private int decision;
	private PImage img;
	private int x;
	private int y;

	public RobotSketch(String name) {
		super(name);
		rnd = new Random();
		loadImage();
		setInitLocation();
	}

	private void loadImage() {
		java.net.URL imgURL = RobotSketch.class.getResource("/Robot.png");
		img = loadImage(imgURL.getPath());
		// image(img, 0, 0);
	}

	private void setInitLocation() {
		x = this.width / 2 - 16;
		y = this.height / 2 - 16;
	}

	@Override
	public void drawFrame() {
		background(255);
		makeDecision();
		move();
		drawRobot();
	}

	private void makeDecision() {
		decision = rnd.nextInt(100);
	}

	private void move() {
		int direction = decision % 4;
		switch (direction) {
		case 0:
			moveLeft();
			break;
		case 1:
			moveRight();
			break;
		case 2:
			moveUp();
			break;
		case 3:
			moveDown();
			break;

		default:
			break;
		}
	}

	private void moveLeft() {
		x -= MOVE_STEP;
		if (x < 0) {
			x = this.width;
		}
	}

	private void moveRight() {
		x += MOVE_STEP;
		if (x > this.width) {
			x = 0;
		}
	}

	private void moveUp() {
		y -= MOVE_STEP;
		if (y < 0) {
			y = this.height;
		}
	}

	private void moveDown() {
		y += MOVE_STEP;
		if (y > this.height) {
			y = 0;
		}
	}

	private void drawRobot() {
		if (null != img) {
			image(img, x, y);
		}
	}

	@Override
	public void updateLogicInterval() {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateDataInOneFrame() {
		// TODO Auto-generated method stub

	}

}
